{include file="public/layout" /}

<body class="bodystyle" style="overflow-y: scroll;">
<div id="toolTipLayer" style="position: absolute; z-index: 9999; display: none; visibility: visible; left: 95px; top: 573px;"></div>
<div id="append_parent"></div>
<div id="ajaxwaitid"></div>
<div class="page">
    <form class="form-horizontal" id="post_form" action="{:url('Member/attr_add')}" method="post">
        <div class="ncap-form-default tab_div_1">
            <dl class="row">
                <dt class="tit">
                    <label for="name"><em>*</em>属性标题</label>
                </dt>
                <dd class="opt">
                    <input type="text" name="title" id="name" class="input-txt">
                    <p class="notic"></p>
                </dd>
            </dl>
            <dl class="row">
                <dt class="tit">
                    <label for="dtype"><em>*</em>属性类型</label>
                </dt>
                <dd class="opt">
                    <select name="dtype" id="dtype">
                    {volist name='$field' id='vo'}
                        <option value="{$vo.name}" data-ifoption="{$vo.ifoption|default=0}">
                            {$vo.title}
                        </option>
                    {/volist}
                    </select>
                    <span class="err"></span>
                    <p class="notic"></p>
                </dd>
            </dl>
            <dl class="row" id="dl_dfvalue">
                <dt class="tit">
                    <label id="label_dfvalue">默认值</label>
                </dt>
                <dd class="opt">
                    <textarea rows="5" cols="60" id="dfvalue" name="dfvalue" placeholder="如果定义字段类型为下拉框、单选项、多选项时，此处填写被选择的项目(用“,”分开，如“男,女,人妖”)。" style="height:60px;"></textarea>
                    <span class="err"></span>
                    <p class="notic">如果定义属性类型为下拉框、单选项、多选项时，此处填写被选择的项目(用“,”分开，如“男,女,人妖”)。</p>
                </dd>
            </dl>
            <dl class="row">
                <dt class="tit">
                    <label>是否禁用</label>
                </dt>
                <dd class="opt">
                    <div class="onoff">
                        <label for="is_hidden0" class="cb-enable">是</label>
                        <label for="is_hidden1" class="cb-disable selected">否</label>
                        <input id="is_hidden0" name="is_hidden" value="1" type="radio">
                        <input id="is_hidden1" name="is_hidden" value="0" type="radio" checked="checked">
                    </div>
                    <p class="notic"></p>
                </dd>
            </dl>
            <dl class="row">
                <dt class="tit">
                    <label>是否必填</label>
                </dt>
                <dd class="opt">
                    <div class="onoff">
                        <label for="is_required1" class="cb-enable {if condition='$info.is_required eq 1'}selected{/if}">是</label>
                        <label for="is_required0" class="cb-disable {if condition='empty($info.is_required)'}selected{/if}">否</label>
                        <input id="is_required1" name="is_required" value="1" type="radio" {if condition="$info.is_required eq 1"} checked="checked"{/if}>
                        <input id="is_required0" name="is_required" value="0" type="radio" {if condition="empty($info.is_required)"} checked="checked"{/if}>
                    </div>
                    <p class="notic">只针对前台的会员属性有效。</p>
                </dd>
            </dl>
        </div>
        <!-- 常规选项 -->
        <div class="ncap-form-default">
            <div class="bot">
                <a href="JavaScript:void(0);" onclick="check_submit();" class="ncap-btn-big ncap-btn-green" id="submitBtn">确认提交</a>
            </div>
        </div>
    </form>
</div>

<script type="text/javascript">
    $(function(){
        dtype_change($('#dtype'));
        $('#dtype').change(function(){
            dtype_change(this);
        });

        function dtype_change(obj){
            var dtype = $(obj).val();
            var ifoption = $(obj).find('option:selected').data('ifoption');
            if (0 <= $.inArray(dtype, ['datetime','switch','img','imgs','file'])) {
                $('#dl_dfvalue').hide();
            } else {
                if (1 == ifoption) {
                    $('#label_dfvalue').html('<em>*</em>默认值');
                } else {
                    $('#label_dfvalue').html('默认值');
                }
                $('#dl_dfvalue').show();
            }
        }
    });

    var parentObj = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
    
    function check_submit(){
        if($('input[name="name"]').val() == ''){
            showErrorMsg('属性标题不能为空！');
            $('input[name=name]').focus();
            return false;
        }
        if($('#dtype').val() == ''){
            showErrorMsg('请选择属性类型！');
            $('input[name=dtype]').focus();
            return false;
        } else {
            var ifoption = $('#dtype').find('option:selected').data('ifoption');
            if (1 == ifoption) {
                if ($.trim($('#dfvalue').val()) == '') {
                    showErrorMsg('默认值不能为空！');
                    $('#dfvalue').focus();
                    return false;
                }
            }
        }
        if($('#dtype').val() == 'radio' || $('#dtype').val() == 'checkbox' || $('#dtype').val() == 'select'){
            var data = $.trim($('#dfvalue').val());
            data = data.split(',');
            for(var i = 0;i < data.length ;i++) {
                for(var j = i+1;j < data.length;j++) {
                    if ($.trim(data[i]) == $.trim(data [j])){
                        showErrorMsg('默认值不能含有相同的值！');
                        $('textarea[name=dfvalue]').focus();
                        return false;
                    }
                }
            }
        }
        layer_loading('正在处理');
        $.ajax({
            type : 'post',
            url : "{:url('Member/attr_add', ['_ajax'=>1])}",
            data : $('#post_form').serialize(),
            dataType : 'json',
            success : function(res){
                layer.closeAll();
                if(res.code == 1){
                    var _parent = parent;
                    _parent.layer.close(parentObj);
                    _parent.layer.msg(res.msg, {shade: 0.3, time: 1000}, function(){
                        _parent.window.location.reload();
                    });
                }else{
                    showErrorMsg(res.msg);
                }
            },
            error: function(e){
                layer.closeAll();
                layer.alert(e.responseText, {icon: 5, title:false});
            }
        });
    }
</script>

{include file="public/footer" /}